<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
      <link rel="stylesheet" type="text/css" href="style/other/easyui.css">
    <link rel="stylesheet" type="text/css" href="style/other/icon.css">
    <link rel="stylesheet" type="text/css" href="style/other/demo.css">
    <script type="text/javascript" src="style/js/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" src="style/js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="style/js/my.js"></script>

<style type="text/css">
.file {
	position: relative;
	display: inline-block;
	border: 1px solid #99D3F5;
	border-radius: 4px;
	padding: 4px 12px;
	overflow: hidden;
	color: #1E88C7;
	text-decoration: none;
	text-indent: 0;
	line-height: 20px;
}

.file input {
	position: absolute;
	font-size: 100px;
	right: 0;
	top: 0;
	opacity: 0;
}
.img {width: 150px;height: 150px;padding-left: 2px; }
</style>

</head>
<body>
	<script type="text/javascript">
	$(function(){
		
     $('#wxt').tree({
    	  //tree会自动ajax后台获取json格式数据(需要自己后台拼接)  
          url:'<%=basePath%>admin/menu_tree.action', 
          onClick:function(node){
                alert(node.text+" - "); //获取点击文本的数据
          }
     });
     
     $("#sub").click(function(){
 		
 		getChecked();
 		
 		$("#im").html("");
 		$("#count").html(0);
 		if (g == '') {alert("请勾选群号");return false;}
 		if (u == '') {alert("请勾选群号");return false;}
 		if ($("#msg").val() == '') {alert("内容不能为空");return false;}
 		var userlist = u;
 		var grouplist = g;
 		var sendJson = s;
 		 u= '';
 		 g='';
 		 s = "[";
             var AjaxURL="<%=path%>/admin/send_sendGroupMsG.action";
             
             $.ajax({
             	url:AjaxURL,
             	 type:"post",
             	 data:{content:$("#msg").val(),users:userlist,groups:grouplist,sendJson:sendJson},
             	success:function(data){ 
             		var msg=document.getElementById("msg").value;	
             		//alert(getNowFormatDate()+'\n'+$("#p").text());
             		$("#ul").append(getNowFormatDate()+'\n'+"<li id='2'>"+msg+"</li>");
             		
             		//清除发送信息框
             		document.getElementById("msg").value = "";
             		
             		 $("#sends").html("正在发送"); 
             		 
             		setInterval(getresult,2000);  
             	},
             	error:function(data){
             	}
             
             });
             
     
 	});
     
     $("#fileuplad").change(function(){
 		getChecked();
 		if (g == '') {alert("请勾选群号");return false;}
 		if (u == '') {alert("请勾选群号");return false;}
 		var userlist = u;
 		var grouplist = g;
 		var sendJson = s;
 		 u= '';
 		 g='';
 		s = "[";
 		   $("#im").html("");
 		   $("#count").html(0);
 			
             var AjaxURL='<%=path%>/admin/send_sendGroupFileMsG.action?users='+userlist+'&groups='+grouplist+'&sendJson='+sendJson;   
             var formData = new FormData(); 
             formData.append("fileuplad", $("#fileuplad")[0].files[0]);
             
             $.ajax({
             	url:AjaxURL,
             	type:"post",
             	 async: false,  
                  cache: false,  
                  contentType: false,  
                  processData: false, 
             	data:formData,
             	success:function(data){  
             		  $("#fileuplad").html(""); 
             		 $("#sends").html("正在发送"); 
             		 var s= data.split("\\");

             		$("#ul").append(getNowFormatDate()+'<p>'+"<img id='img' class='img'  src=\""+s[s.length-1]+"\">");
             		
	            		
             	setInterval(getresult,2000);
             	

             	},
             	error:function(data){}
             
             });
             
     
 	});
     
     
     
	});
	/*************获取当前选中的微信群相关信息*********************/
	var s = "["; //存放当前当前点击群号信息json
	var u = '';    //存放当前当前点击群号名称
	var g = '';    //存放当前当前点击群号名称

	function getChecked(){
		var sum = 0; //统计当前点击群号个数
		
			var nodes = $('#wxt').tree('getChecked');
			
			for(var i=0; i<nodes.length; i++){
				s+="{\"subName\":\""+nodes[i].subName+"\",";
				s+="\"polName\":\""+nodes[i].polName+"\",";
				s+="\"userNickName\":\""+nodes[i].userNickName+"\",";
				s+="\"groupNickName\":\""+nodes[i].text+"\",";
				s+="\"content\":\""+$("#msg").val()+"\"}";
				if(i ==nodes.length-1 ){
				s+="]";
				}else{
				s+=",";	
				}
				if (g != '') g += ',';	
				
				g += nodes[i].groupname;
				
				//遍历所选的群号判断所属微信号
				var str = u.split(",");
				var flag = false;
				for(var j = 0;j<str.length;j++){	
					if(str[j]==nodes[i].username){
						flag = true;
						break;
					}

				}
				//判断所属微信号
				if(!flag){
					if (u != '') u += ',';
					u += nodes[i].username;
					
				}
				str='';	
			}
			

			sum = nodes.length;
			 $("#sum").html(sum);
		    sum = 0;
		}
	/**************************************/
	function send_msg(){
		
		
		var msg=document.getElementById("content").value;	
		//alert(getNowFormatDate()+'\n'+$("#p").text());
		$("#ul").append(getNowFormatDate()+'\n'+"<li id='2'>"+msg+"</li>");
		
		//清除发送信息框
		document.getElementById("msg").value = "";
		}
	

	var datassend='';
	function getresult(){

		 var AjaxURL="admin/send_getResult.action"; 
		
		 if($("#sum").html()!=$("#count").html()){
			 
		    $.ajax({url:AjaxURL,type:"post",
		            	success:function(data){
		            		
		            		 var json = eval($.parseJSON(data)); //数组
		            		 $("#count").html(json["SendSuccessCount"]);
		            		 if($("#sum").html()==$("#count").html()){
		            			 $("#sends").html("已发送成功");
		            		 }
			            		
		            		 if(datassend.split(":")[0]!=json["name"].split(":")[0]){
		            			
		            		 $("#statssend").html($("#statssend").html()+"<li>"+json["name"]+":"+getNowFormatDate()+"<li>");
		            		 }
		            		 datassend = json["name"];
		            		 
		            		
		            		
		            	},
		            	error:function(data){
		            	}
		            
		            });
		}
	 
	
	}
	</script>


<div class="easyui-layout" fit="true" style="height:600px;">

    	<div region="east" split="true"  title="监控台" style="width:250px;">
			<ul class="easyui-tree" id="statssend"></ul>
		</div>
		
		<div region="west" split="true" title="微信号" style="width:250px;">
			<ul id="wxt" class="easyui-tree" checkbox="true"></ul>
		</div>
		
	
		
		<div id="content" region="center" title="发送信息" style="padding:20px;width:auto">
			
			<div style="margin:10px 0;"></div>
			<div id="p" class="easyui-panel" title="信息"
				style="width:auto;height:400px;padding:2px;overflow:auto">
				
				<ul id="ul" style="list-style-type:none;margin:0;padding:0;">
				</ul>
			</div>
			<div id="" class="easyui-panel" style="width:auto;padding:5px;height: 40px">
			
			<table ><tr>
			<td><form id="uploadForm" name="uploadForm" method="post" enctype="multipart/form-data">
								<a href="javascript:void(0);" class="file"
									style="position: relative;
	display: inline-block;
	border: 1px solid #99D3F5;
	padding:1px 1px;
		overflow: hidden;
	color: #1E88C7;
	text-decoration: none;
	text-indent: 0;
	"><img src="style/js/themes/icons/small_picture.png" width="20px" />
									<input type="file" id="fileuplad" name="fileuplad" 
									style="position: absolute;
	               font-size: 100px;
	                right: 0;
	               top: 0;
	             opacity: 0;
	              " />
								</a>
							</form>
				</td>
			<td style="width: 60%" align="right">发送状态:<span id="sends"></span> </td>
			<td style="width: 20%"align="right">已发送:<span id="count">0</span> </td>
			<td style="width: 20%" align="right">总数:<span id="sum">0</span></td>
			</tr></table>
				
			</div>
			<div id="" class="easyui-panel" style="width:auto;">
				<textarea id="msg" name="msg" 
					style="width:99.4%;height:150px;"></textarea>
			</div>
			<div id="" class="easyui-panel"
				style="width:auto;text-align:right;padding:2px;">
				<a href="javascript:void(0);"   id="sub" name="sub"
					class="easyui-linkbutton">发送</a>
			</div>
		</div>
    	</div>

</body>
</html>